package org.four.user.service.impl;

import org.four.common.model.*;
import org.four.common.utils.Md5Util;
import org.four.user.mapper.UserMapper;
import org.four.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;


    @Override
    public List<NavBean> findTree() {
        Integer id=0;
        return findTrees(id);
    }

    //树
    private List<NavBean> findTrees(Integer id) {
        List<NavBean> list=userMapper.queryTrees(id);
        for (NavBean treeBean : list) {
            Integer id2 = treeBean.getId();
            List<NavBean> findTree = findTrees(id2);
            if(findTree!=null && findTree.size()>0){
                treeBean.setChildren(findTree);
                treeBean.setSpread(true);
            }else {
                treeBean.setSpread(false);
            }
        }
        return list;
    }

    //查询一条
    @Override
    public UserBean testAccount(String account) {
        return userMapper.testAccount(account);
    }

    //注册
    @Override
    public void saveUser(UserBean userBean) {
        userMapper.saveUser(userBean);
    }

    //登陆
    @Override
    public Map login(UserBean userBean, HttpServletRequest request) {
        HashMap<String, Object> result = new HashMap<>();
        UserBean user =userMapper.queryUserByAccount(userBean.getAccount());
        if(user==null){
            result.put("code",1);
            result.put("msg","账号或者密码错误");
            return result;
        }
        String password = Md5Util.getMd532(userBean.getPassword());
        if(!user.getPassword().equals(password)){
            result.put("code",1);
            result.put("msg","账号或者密码错误");
            return result;
        }
        result.put("code",0);
        result.put("msg","登录成功");
        HttpSession session = request.getSession();
        session.setAttribute(session.getId(),user);
        return result;
    }

    //新增简历
    @Override
    public void addUser(ZhiweiBean zhiweiBean) {
        userMapper.addUser(zhiweiBean);
    }

    //修改简历
    @Override
    public void upda(ProductBean productBean) {
        userMapper.upda(productBean);
    }

    //个人中心分页查询商品
    @Override
    public LayuiPage queryProduct(int limit, int page,Integer id) {
        int total = userMapper.queryProductCount(id);
        int st = (page-1)*limit;
        List<ProductBean> list = userMapper.queryProduct(st,limit,id);
        LayuiPage layuiPage = new LayuiPage();
        layuiPage.setCode(0);
        layuiPage.setCount(total);
        layuiPage.setMsg("");
        layuiPage.setData(list);
        return layuiPage;
    }

    //简历回显
    @Override
    public ProductBean findProductInfo(Integer id) {
        return userMapper.findProductInfo(id);
    }

    //查询投递
    @Override
    public LayuiPage toudi(int limit, int page, Integer id) {
        int total = userMapper.querytoucount(id);
        int st = (page-1)*limit;
        List<ZhaoPinBean> list = userMapper.querytoudi(st,limit,id);
        LayuiPage layuiPage = new LayuiPage();
        layuiPage.setCode(0);
        layuiPage.setCount(total);
        layuiPage.setMsg("");
        layuiPage.setData(list);
        return layuiPage;
    }

    //个人中心分页查询商品
    @Override
    public LayuiPage queryProductcjq(Integer id) {
            List<UserBean> list = userMapper.queryProductcjq1(id);
            LayuiPage layuiPage = new LayuiPage();
            layuiPage.setCode(0);
            layuiPage.setCount(1);
            layuiPage.setMsg("成功");
            layuiPage.setData(list);
        return layuiPage;
        }

    //收藏分页查询商品
    @Override
    public LayuiPage queryzhiwei(int limit, int page) {
        int total = userMapper.queryProductCount2();
        int st = (page-1)*limit;
        List<ZhiweiBean> list = userMapper.queryzhiwei(st,limit);
        LayuiPage layuiPage = new LayuiPage();
        layuiPage.setCode(0);
        layuiPage.setCount(total);
        layuiPage.setMsg("成功");
        layuiPage.setData(list);
        return layuiPage;
    }

    //订阅分页查询商品
    @Override
    public LayuiPage querydingyue(int limit, int page,ZhiweiBean zhiweiBean) {
        int total = userMapper.queryProductCount3(zhiweiBean);
        int st = (page-1)*limit;
        List<ZhiweiBean> list = userMapper.queryzhiwei3(st,limit,zhiweiBean);
        LayuiPage layuiPage = new LayuiPage();
        layuiPage.setCode(0);
        layuiPage.setCount(total);
        layuiPage.setMsg("成功");
        layuiPage.setData(list);
        return layuiPage;
    }

    //删除收藏
    @Override
    public void delcjq(Integer[] id) {
        userMapper.delcjq(id);
    }

    //删除订阅
    @Override
    public void del2(Integer[] id) {
        userMapper.del2(id);
    }

    //新增订阅
    @Override
    public void addProductcjq(ZhiweiBean zhiweiBean) {
        userMapper.addProductcjq(zhiweiBean);
    }

    //修改订阅
    @Override
    public void updacjq(@RequestBody ZhiweiBean zhiweiBean) {
        userMapper.updacjq(zhiweiBean);
    }

    //账号设置
    @Override
    public void zhanggai(UserBean userBean) {
        userMapper.zhanggai(userBean);
    }

    //账号设置查询
    @Override
    public LayuiPage haolist(int limit, int page) {
        int total = userMapper.haolistcount();
        int st = (page-1)*limit;
        List<UserBean> list = userMapper.haolist(st,limit);
        LayuiPage layuiPage = new LayuiPage();
        layuiPage.setCode(0);
        layuiPage.setCount(total);
        layuiPage.setMsg("成功");
        layuiPage.setData(list);
        return layuiPage;
    }

    //测试
    @Override
    public LayuiPage queryList(Integer page, Integer limit, ZhaoPinBean zhaoPinBean) {
        Integer start =(page-1)*limit;
        Integer end=limit;
        Integer count=userMapper.queryCount(zhaoPinBean);
        List<ZhaoPinBean> list=userMapper.queryList(start,end,zhaoPinBean);
        LayuiPage layuiPage = new LayuiPage();
        layuiPage.setMsg("成功");
        layuiPage.setData(list);
        layuiPage.setCount(count);
        layuiPage.setCode(0);
        return layuiPage;
    }

    //测试 精确查询
    @Override
    public List<ComUserBean> queryType() {
        return userMapper.queryType();
    }

    //查询招聘的岗位
    @Override
    public List<ZhaoPinBean> findZhaoPin() {

        return userMapper.findZhaoPin();
    }

    //跳转
    @Override
    public List<ZhaoPinBean> findProductListImg() {
        return userMapper.findProductListImg();
    }

    //根据id查询商品详情
    @Override
    public List<Map> queryProductById(Integer id) {
        List<Map> maps = userMapper.queryProductById(id);
        System.out.println(maps);
        return maps;
    }

    //订阅回显
    @Override
    public ZhiweiBean findyue(Integer id) {
        return userMapper.findyue(id);
    }

    //投递简历
    @Override
    public void addli(ToudiBean toudiBean) {
        userMapper.addli(toudiBean);
    }

    @Override
    public void productState(Integer id) {
        userMapper.productState(id);
    }

    @Override
    public LayuiPage JingYanBean(Integer id) {
        LayuiPage objectLayuiPage = new LayuiPage<>();
        List<JingYanBean> jingYanBeans = userMapper.JingYanBean(id);
        objectLayuiPage.setMsg("成功");
        objectLayuiPage.setCode(0);
        objectLayuiPage.setData(jingYanBeans);
        return objectLayuiPage;
    }

    @Override
    public void addXiangmu(JingYanBean jingYanBean) {
        userMapper.addXiangmu(jingYanBean);
    }

    @Override
    public void delXiangMu(Integer id) {
        userMapper.delXiangMu(id);
    }

    @Override
    public void addtoudi(Integer comid, Integer userid) {
        userMapper.addtoudi(comid,userid);
    }

    /**
     * 根据userId 查询具体的数据
     * @param id
     * @return
     */
    @Override
    public UserBean findQueryUserList(Integer id) {
        UserBean queryUserList = userMapper.findQueryUserList(id);
        return queryUserList;
    }

    /**
     * 工作经历表
     * @param id
     * @return
     */
    @Override
    public ResultBean findUserGongZuoJingLi(Integer id) {
        List<GongZuoBean> userGongZuoJingYan =  userMapper.findUserGongZuoJingLi(id);
        ResultBean resultBean = new ResultBean();
        resultBean.setCode(0);
        resultBean.setMsg("成功");
        resultBean.setData(userGongZuoJingYan);
        return resultBean;
    }

    //修改用户登录密码
    @Override
    public void updatePassWord(Integer id, String md532) {
        userMapper.updatePassWord(id,md532);
    }

    //教育情况
    @Override
    public ResultBean findJiaoYu(Integer id) {
        List<JiaoYuBen> jiaoYuBens = userMapper.findConypanyMessage(id);
        ResultBean resultBean = new ResultBean();
        resultBean.setMsg("成功");
        resultBean.setCode(0);
        resultBean.setData(jiaoYuBens);
        return resultBean;
    }

    //删除教育经历
    @Override
    public void delJiaoYu(Integer id) {
        userMapper.delJiaoYu(id);
    }

    //新增教育经历
    @Override
    public void saveJiaoYu(JiaoYuBen jiaoYuBen) {
        userMapper.saveJiaoYu(jiaoYuBen);
    }

    //删除工作经历
    @Override
    public void delGongZuo(Integer id) {
        userMapper.delGongZuo(id);
    }

    //新增工作经历
    @Override
    public void saveGongZuo(GongZuoBean gongZuoBean) {
        userMapper.saveGongZuo(gongZuoBean);
    }

    @Override
    public List<Map> queryjituan(Integer comid) {
        return userMapper.queryjituan(comid);
    }


}
